package com.sqx.modules.task.controller.app;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.orders.dao.OrdersDao;
import com.sqx.modules.orders.entity.Orders;
import com.sqx.modules.task.dao.HelpOrderDao;
import com.sqx.modules.task.entity.HelpOrder;
import com.sqx.modules.task.entity.HelpTake;
import com.sqx.modules.task.service.HelpOrderService;
import com.sqx.modules.task.service.HelpTakeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

/**
 * @author fang
 * @date 2021/1/8
 */
@RestController
@Api(value = "跑腿订单", tags = {"跑腿订单"})
@RequestMapping(value = "/app/help")
public class AppHelpController {

    @Autowired
    private HelpOrderService helpOrderService;

    @Resource
    private OrdersDao ordersDao;

    @Resource
    private HelpOrderDao helpOrderDao;

    @Autowired
    private HelpTakeService helpTakeService;

    @Autowired
    private CommonInfoService commonInfoService;

    /**
     * 订单播报
     * @return
     */
    @GetMapping("/orderBroadcast")
    @ApiOperation("订单播报")
    private Result orderBroadcast(){
        return helpOrderService.orderBroadcast();
    }

    @Login
    @PostMapping("/saveHelpOrder")
    @ApiOperation("发布跑腿订单")
    @ResponseBody
    public Result saveHelpOrder(@RequestBody HelpOrder helpOrder, @RequestAttribute Long userId) {
        helpOrder.setUserId(userId);
        return helpOrderService.saveBody(helpOrder);
    }


    @Login
    @PostMapping("/updateHelpOrderByStatus/{ids}/{status}/{content}")
    @ApiOperation("审核跑腿订单")
    @ResponseBody
    public Result updateHelpOrderByStatus(@PathVariable("ids") String ids, @PathVariable("status") Integer status, @PathVariable("content") String content) {
        return helpOrderService.updateHelpOrderByStatus(ids, status, content);
    }

    @Login
    @PostMapping("/updateHelpOrder")
    @ApiOperation("完善需求")
    @ResponseBody
    public Result updateHelpOrder(@RequestBody HelpOrder helpOrder) {
        return helpOrderService.updateHelpOrderById(helpOrder);
    }

    @Login
    @PostMapping("/payMoney")
    @ApiOperation("零钱去支付")
    public Result payMoney(Long ordersId) {
        return helpOrderService.payMoney(ordersId);
    }

    @Login
    @PostMapping("/deleteHelpOrder")
    @ApiOperation("删除")
    @ResponseBody
    public Result deleteHelpOrder(Long helpOrderId) {
        return helpOrderService.deleteById(helpOrderId);
    }

    @Login
    @PostMapping("/outHelpOrder")
    @ApiOperation("下架任务")
    @ResponseBody
    public Result outHelpOrder(Long helpOrderId) {
        return helpOrderService.outHelpOrder(helpOrderId);
    }

    @Login
    @PostMapping("/startHelperOrder")
    @ApiOperation("开始服务")
    public Result startHelperOrder(Long helpOrderId, @RequestAttribute("userId") Long userId, String startTaskImg, String startTaskAddress, String startTaskLongitude, String startTaskLatitude) {
        return helpTakeService.startHelperOrder(helpOrderId, userId, startTaskImg, startTaskAddress, startTaskLongitude, startTaskLatitude);
    }

    @Login
    @PostMapping("/closeOrder")
    @ApiOperation("确认送达")
    @ResponseBody
        public Result closeOrder(Long helpTakeId, Long helpOrderId, String code, String endTaskImg, String endTaskAddress, String endTaskLongitude, String endTaskLatitude) {
        return helpTakeService.closeOrder(helpTakeId, helpOrderId, code, endTaskImg, endTaskAddress, endTaskLongitude, endTaskLatitude, 1);
    }

    @Login
    @PostMapping("/saveHelpTake")
    @ApiOperation("接单")
    @ResponseBody
    public Result saveHelpTake(@RequestBody HelpTake helpTake, @RequestAttribute Long userId) {
        helpTake.setUserId(userId);
        return helpTakeService.saveBody(helpTake);
    }


    @ApiOperation("将订单指派给员工")
    @PostMapping("/distributionOrder")
    public Result distributionOrder(Long companyId, Long takeId) {
        return helpTakeService.distributionOrder(companyId, takeId);
    }

    @ApiOperation("获取指派给员工的订单列表")
    @GetMapping("/getCompanyOrder")
    public Result getCompanyOrder(Integer page, Integer limit, Integer status, Long userId, Long companyId, Long userIds) {
        return Result.success().put("data", helpTakeService.getCompanyOrder(page, limit, status, userId, companyId, userIds));
    }


    @Login
    @PostMapping("/offerOrder")
    @ApiOperation("报价")
    @ResponseBody
    public Result offerOrder(Long id, BigDecimal offerMoney,Integer userType, @RequestAttribute Long userId) {
        return helpTakeService.offerOrder(id, offerMoney,userType, userId);
    }

    @Login
    @PostMapping("/choiceBidding")
    @ApiOperation("选择报价人并支付金额")
    public Result choiceBidding(Long orderId, Long biddingId) {
        return helpOrderService.choiceBidding(orderId, biddingId);
    }

    @Login
    @PostMapping("/endHelpTake")
    @ApiOperation("放弃接单")
    @ResponseBody
    public Result endHelpTake(Long id,Long userId) {
        return helpTakeService.endHelpTake(id,userId);
    }


    @GetMapping("selectNewHelpOrderList")
    @ApiOperation("最新跑腿")
    @ResponseBody
    public Result selectNewHelpOrderList(Integer page, Integer limit, Long gameId, String latitude, String longitude, Integer sort, Integer taskType, Long classifyId, Long userId) {
        return helpOrderService.selectNewHelpOrderList(page, limit, gameId, latitude, longitude, sort, taskType, classifyId, userId);
    }


    @GetMapping("/selectHelpOrderByClassifyList")
    @ApiOperation("根据分类查找跑腿")
    @ResponseBody
    public Result selectHelpOrderByClassifyList(Integer page, Integer limit, Long classifyId, Long gameId) {
        return helpOrderService.selectHelpOrderByClassifyList(page, limit, classifyId, gameId);
    }


    @GetMapping("/selectHelpOrderByNameList")
    @ApiOperation("根据名称模糊查找跑腿")
    @ResponseBody
    public Result selectHelpOrderByContentList(Integer page, Integer limit, String content, Long gameId) {
        return helpOrderService.selectHelpOrderByContentList(page, limit, content, gameId);
    }

    @GetMapping("/selectHelpOrderDetails")
    @ApiOperation("查看跑腿详细信息")
    @ResponseBody
    public Result selectHelpOrderDetails(Long helpOrderId, Long userId, Long userIds,String longitude,String latitude) {
        HelpOrder helpOrder = helpOrderService.selectHelpOrderById(helpOrderId, userId, userIds,longitude,latitude);
        return Result.success().put("data", helpOrder);
    }

    @Login
    @GetMapping("/selectRunHelpOrder")
    @ApiOperation("我的订单-跑腿订单")
    @ResponseBody
    public Result selectRunHelpOrder(Integer page, Integer limit, Integer status, Integer taskType,
                                     String orderNo, String phone, String startTime, String deliveryTime,
                                     @RequestAttribute Long userId,String content,String gameId,String name) {
        return helpTakeService.selectRunHelpOrder(page, limit, status, taskType, orderNo, phone, startTime, deliveryTime, userId,content,gameId,name);
    }

    /*
    * 商家端--我的接单--待服务订单数量and商家自身服务订单数量
    * */
    @Login
    @GetMapping("/selectWaitServiceOrderCount")
    @ApiOperation("商家端--我的接单--待服务订单数量")
    public Result selectWaitServiceOrderCount(@RequestAttribute Long userId) {

        // 根据用户id来获取待服务订单数量
        Integer count = helpOrderDao.selectCount(new LambdaQueryWrapper<HelpOrder>().eq(HelpOrder::getUserId, userId).eq(HelpOrder::getStatus, 2));

        Integer serviceCount = ordersDao.selectCount(new LambdaQueryWrapper<Orders>().eq(Orders::getState, 4).eq(Orders::getOrderTakingUserId, userId));

        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("waitService", ObjectUtil.defaultIfNull(count,0));
        data.put("serviceCount", ObjectUtil.defaultIfNull(serviceCount,0));
        return Result.success().put("data", data);
    }

    @Login
    @GetMapping("/getMyOfferOrder")
    @ApiOperation("我的报价列表")
    public Result getMyOfferOrder(Integer page, Integer limit, Integer status, @RequestAttribute Long userId) {
        return Result.success().put("data", helpTakeService.getMyOfferOrder(page, limit, status, userId));
    }


    @Login
    @GetMapping("/selectCreateHelpOrder")
    @ApiOperation("我的订单-发布订单")
    @ResponseBody
    public Result selectCreateHelpOrder(Integer page, Integer limit, Integer status, @RequestAttribute Long userId, Long gameId) {
        return helpOrderService.selectCreateHelpOrder(page, limit, status, userId, gameId);

    }

    @Login
    @GetMapping("/selectStatusHelpOrder")
    @ApiOperation("查询待审核的订单")
    @ResponseBody
    public Result selectStatusHelpOrder(Integer page, Integer limit, String phone, String content, Integer status, Long gameId, Integer taskType) {
        return helpOrderService.selectStatusHelpOrder(page, limit, phone, content, status, gameId, taskType);

    }

    @Login
    @GetMapping("/selectHelpTakeList")
    @ApiOperation("查询已接单的接单订单")
    @ResponseBody
    public Result selectHelpTakeList(Integer page, Integer limit, Integer status, String phone, Integer taskType, Long rideUserId, String rideName) {
        return helpTakeService.selectRunHelpOrder(page, limit, status, phone, null, null, taskType, rideUserId, rideName);
    }


    @ApiOperation("修改预约时间和备注")
    @PostMapping("/updateTimeAndRemarks")
    public Result updateTimeAndRemarks(Long helpOrderId, String deliveryTime, String remarks) {
        return helpOrderService.updateTimeAndRemarks(helpOrderId, deliveryTime, remarks);
    }

    @ApiOperation("统计订单成交量")
    @GetMapping("/selectHelpOrderCount")
    public Result selectHelpOrderCount() {
        CommonInfo one = commonInfoService.findOne(299);
        int num = Integer.parseInt(one.getValue());
        int count = helpOrderService.count(new QueryWrapper<HelpOrder>().ne("status", 1).ne("status", 5));
        int random = (int) ((5 - 1 + 1) * Math.random() + 1);
        one.setValue(count + num + random + "");
        commonInfoService.update(one);
        return Result.success().put("data", one.getValue());
    }

    @Login
    @ApiOperation("根据员工id统计订单")
    @GetMapping("/getCompanyOrderCount")
    public Result getCompanyOrderCount(@RequestAttribute("userId") Long userId) {
        return Result.success().put("data", helpTakeService.getCompanyOrderCount(userId));
    }

//    @Login
//    @GetMapping("/selectNowDayHelpOrders")
//    @ApiOperation("万能任务今日订单")
//    public Result selectNowDayOrders(Integer page, Integer limit, @RequestAttribute Long userId) {
//        return Result.success().put("data", helpOrderService.selectHelpOrder(page, limit, userId));
//    }


}
